$('.has_children').addClass('dropdown');
$('.has_children a').addClass('dropdown-toggle').attr("data-toggle","dropdown");
$('.has_children > a').append(' <b class="caret"></b>');
$('.has_children > ul').addClass('dropdown-menu');

$('.success-box').addClass('alert alert-success').prepend('<button class="close" data-dismiss="alert">×</button>');
$('.error-box').addClass('alert alert-error').prepend('<button class="close" data-dismiss="alert">×</button>');
$('.notice-box').addClass('alert alert-block').prepend('<button class="close" data-dismiss="alert">×</button>');

$('#navaffix').affix();

$.wait = function(time) {
  return $.Deferred(function(dfd) {
    setTimeout(dfd.resolve, time);
  });
}
function set_disable()
{
  $('.testmain_clouds g').each(function(){
            $(this).attr('c_attr','');
            $(this).css('fill','#333333');
            var c_node=$(this).attr('class').split(' ');
            $('#'+c_node[1]).removeClass('active1'); 
            $('.testmain_status').text('');
        });  
}

var actions="false";
var select_rad=0;

function make_anime(selected_item)
{
    if (actions=="true") {
  setTimeout(function() {
                var c_rad=selected_item.attr('r');
                var main_rad=selected_item.next().attr('r');
                if (c_rad<=main_rad) {
                    selected_item.attr('r',select_rad);
                } else {                
                c_rad=c_rad-4;
                selected_item.attr('r',c_rad);    
                }                
                
                make_anime(selected_item);  
}, 100);  
}
}

function refresh_svg()
{
    
        
  var count_node=0;
  $('.testmain_clouds g').each(function(){
    
    
    if ($(this).attr('class')=='link') { 
        
    } else {
        count_node=$(this).attr('data-id');
    $(this).attr('class','node node-'+count_node);
    
    }
    
    $(this).click(function(){
        
        var in_edit=$('.testmain_edit a i').attr('class');
        $('.testmain_clouds g').attr('c_attr','');
        
        $('.testmain_clouds g .ifselect').removeClass('ifselect');
        $(this).find('circle').eq(0).addClass('ifselect');
        
        if (in_edit=='icon-edit ico_white') {
            $(this).attr('c_attr','active');
            } else {
            $(this).attr('c_attr','');
            }
        
        $('.testmain_clouds g .ifselect').hide();
        
        var c_attr=$(this).attr('c_attr');
        if (c_attr=='active') {
            
            var c_node=$(this).attr('class').split(' ');
            $('#'+c_node[1]).removeClass('active1'); 
            $('.testmain_status').text('');
            var selected_item=$(this).find('.ifselect');
            selected_item.show();
             actions="true";
             select_rad=selected_item.attr('r');
           //make_anime(selected_item);  
            
        } else {
            set_disable();
        
            $('.testmain_back').show();
        
             //load clouds children
            var node_class=$(this).attr('class').split(' ');
            var node_level=$(this).attr('level_node');
            $('.testmain_back').removeClass('level3');
            $('.testmain_back').removeClass('level2');
            $('.testmain_back').removeClass('level1');
            
            $('.testmain_back').addClass('level'+node_level);
                $('.testmain_clouds svg').remove();
                get_graf('.testmain_clouds','http://cytellect.com/addons/shared_addons/themes/bs/js/'+node_class[1]+'.json','view_child');
                
                $.wait(1000).then(function(){
                refresh_svg();
                });
            
        }
    });
    
    $(this).mouseover(function(){
       var c_node=$(this).attr('class').split(' ');
       $('#'+c_node[1]).addClass('active'); 
       $('.test_main_right').hide();
    });
    
    $(this).mouseout(function(){
       var c_node=$(this).attr('class').split(' ');
       $('#'+c_node[1]).removeClass('active'); 
    });
  });
  
  
}

function update_class()
{

}

$.wait(1000).then(function(){

  refresh_svg();
  
  
  if (document.getElementById('main_body')!=null) {
    get_graf_new();
  }
  
  if (document.getElementById('main_body_v01')!=null) {
    get_graf_v01();
  }
  
  
   
    
  $('.new_row_count').text($('.main_row_count').text());
    
  $('.testmain_edit a').click(function(){
        actions="false";
    if ($('.testmain_add').hasClass('view')) {
        $('.testmain_add').removeClass('view');
        $('.testmain_remove').removeClass('view');
        $('.testmain_edit_node').removeClass('view');
        
        $('.testmain_edit a .icon-edit').attr('class','icon-edit');
        $('.testmain_clouds g .ifselect').hide();
        set_disable();
        
    } else {
        $('.testmain_add').addClass('view');
        $('.testmain_remove').addClass('view');
        $('.testmain_edit_node').addClass('view');
        $('.testmain_edit a .icon-edit').attr('class','icon-edit ico_white');
    }
    return false;
  });
  
  $('.testmain_remove').click(function(){

  });
  
  $('.testmain_edit_node').click(function(){
    
    $('.edit_head').text('Редактировать облако');
    });
  
  $('.testmain_add').click(function(){
    
    $('.edit_head').text('Добавить облако');
    });
  
  $('.btn.btn-primary').click(function(){   
    
    $('#myModalnew').modal('hide');
    
    var new_row_count=parseInt($('.new_row_count').text());
    new_row_count++;
    
    $('.new_row_count').text(new_row_count);
    var cloud_name=$('.cloud_name').val();
    
    $('.all_new_row').append('<div class="new_row-'+new_row_count+'">'+cloud_name+'</div>');
    $('.testmain_clouds svg').remove();
    get_graf('.testmain_clouds','http://cytellect.com/addons/shared_addons/themes/bs/js/graph.json','add_new');
    
    $.wait(1000).then(function(){
    refresh_svg();
    });
    
  });
  
  $('.btn.btn-primary-del').click(function(){
    $('#myModalnew_del').modal('hide');
    });
  
  });
  
  $('.testmain_back a').click(function(){
    
    if ($('.testmain_back').hasClass('level1')) {
        
        $('.testmain_back').removeClass('level1');
        
    $('.testmain_back').hide();
    $('.testmain_clouds svg').remove();
    get_graf('.testmain_clouds','http://cytellect.com/addons/shared_addons/themes/bs/js/graph.json','');
    $.wait(1000).then(function(){
    refresh_svg();
    });    
    }
    
    if ($('.testmain_back').hasClass('level2')) {
    
    $('.testmain_back').removeClass('level2');
    $('.testmain_back').addClass('level1');
    
    $('.testmain_clouds svg').remove();
    get_graf('.testmain_clouds','http://cytellect.com/addons/shared_addons/themes/bs/js/node-10.json','view_child');
    $.wait(1000).then(function(){
    refresh_svg();
    });    
    }
    
    if ($('.testmain_back').hasClass('level3')) {
    
    $('.testmain_back').removeClass('level3');
    $('.testmain_back').addClass('level2');
    
    $('.testmain_clouds svg').remove();
    get_graf('.testmain_clouds','http://cytellect.com/addons/shared_addons/themes/bs/js/node-10.json','view_child');
    $.wait(1000).then(function(){
    refresh_svg();
    });    
    }
    
    if ($('.testmain_back').hasClass('level4')) {
    
    $('.testmain_back').removeClass('level4');
    $('.testmain_back').addClass('level2');
    
    $('.testmain_clouds svg').remove();
    get_graf('.testmain_clouds','http://cytellect.com/addons/shared_addons/themes/bs/js/node-10.json','view_child');
    $.wait(1000).then(function(){
    refresh_svg();
    });    
    }
    
    return false;
  });
  
 $('.testmain_back_v01 a').click(function(){
        $('.link_0_2').hide();
            $('.level-2').hide();
            $('.node-11').show();
            $('.node-10').show(); 
            return false;
 });
 
 $('.testmain_back_v02 a').click(function(){
        $('.link_0_2').show();
            $('.level-2').show();
            $('.node-10').show();
            $('.node-13').show();
            $('.node-14').show();
            $('.link_2_3').hide();
            $('.level-3').hide();
            $('.testmain_back_v02').hide();
            $('.testmain_back_v01').show();
            return false;
 });
 
 $('.page h2').click(function(){
    
    
    d3.selectAll(".node")
      .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
      
   });
